using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Serialization;
using Utils;

public class RoomController : MonoBehaviour
{
    private GameObject monsters;

    private Transform monsterNearest;
    private Transform[] monstersPos;

    private Collider2D collider2D;

    public bool inFightState;
    public Monster boss;

    public Animator fenceAni;
    private static readonly int Up = Animator.StringToHash("up");

    public int roomNumber;
    
    public RiddleController riddleCtr;

    public GameObject treasuresGO;

    void Awake()
    {
        if (GameController.Instance().archiveCtr.TempLevelData.gameType == GameType.Dungeon)
        {
            fenceAni = UITool.GetChildGameObject("Fence", gameObject).GetComponent<Animator>();
            collider2D = GetComponent<BoxCollider2D>();
        }

        treasuresGO = UITool.GetChildGameObject("Treasures", gameObject);
        monsters = UITool.GetChildGameObject("Monsters", gameObject);
        riddleCtr = GetComponentInChildren<RiddleController>();
    }

    void Update()
    {
        if (GameController.Instance().archiveCtr.TempLevelData.gameType == GameType.Dungeon)
        {
            AllMonstersDie();
        }

        ChangePlayerAndWeaponForwardByNearestEnemy();
    }

    #region 获取最近的敌人(敌人在侦测范围内) ChangePlayerAndWeaponForwardByNearestMonster

    private void ChangePlayerAndWeaponForwardByNearestEnemy()
    {
        if (inFightState)
        {
            var playerPos = GameController.Instance().playerCtr.transform.position;

            var dis = Mathf.Infinity;
            foreach (Transform enemy in monsters.transform)
            {
                var diff = enemy.position - playerPos;
                var curDis = diff.sqrMagnitude; //平方消除负数
                if (curDis < dis)
                {
                    monsterNearest = enemy;
                    dis = curDis;
                }
            }

            // print(monsterNearest.name + "-- 最近的敌人");
            GameController.Instance().playerCtr.ChangeForwardByNearestEnemy(monsterNearest.position, dis);
        }
    }

    #endregion

    #region 该房间怪物已全部击杀 AllMonstersDie

    private void AllMonstersDie()
    {
        if (inFightState && monsters.transform.childCount == 0)
        {
            fenceAni.SetBool(Up, false);
            inFightState = false;
            // Destroy(collider2D);

            GameController.Instance().FSM.CurPlayerState = PlayerState.NotFight;
            GameController.Instance().weaponCtr.Weapon.ResetForward();
        }
    }

    #endregion


    private void OnTriggerEnter2D(Collider2D other)
    {
        if (other.transform.tag.Equals("Player") && other.name.Equals("InValidArea"))
        {
            #region 玩家初次进入怪物房间 OnTriggerEnter2D

            GameController.Instance().smallMap.ChangeColorByIndex(roomNumber);
            if (monsters.transform.childCount != 0)
            {
                fenceAni.SetBool(Up, true);
                inFightState = true;

                if (boss)
                {
                    GameController.Instance().panelCtr.LoadBossPanel(boss);
                }
            }

            #endregion

            #region 玩家初次进入解谜房间
            
            if (riddleCtr)
            {
                riddleCtr.CheckRiddles();
                if (!riddleCtr.isSolved)
                {
                    fenceAni.SetBool(Up, true);
                }
            }

            #endregion
        }


        #region 玩家未进入怪物房间,但发射的子弹进入了房间

        if (!inFightState && other.GetComponent<Bullet>() != null)
        {
            if (monsters.transform.childCount == 0)
            {
                return;
            }
            
            other.gameObject.GetComponent<Bullet>().ResetSelf();
            // Destroy(other.gameObject);
        }

        #endregion
    }
}